/**
 * %u 字符串替换
 */
export const formatStr = (str: string, ...args: string[]) => {
  let fmtstr = "";
  args.forEach((item) => {
    fmtstr = str.replace("%u", item);
  });
  return fmtstr;
};

// 依次替代

/**
 * 字符模版替换，模版字符正则 默认 /\(\((\w+)\)\)/g
 * 可以自定义进行修改
 */
export const templateReplace = (str: string, data: Record<string, any>, regex: RegExp = /\(\((\w+)\)\)/g): string => {
  if (typeof str !== "string") {
    throw new Error("First argument must be a string.");
  }
  if (typeof data !== "object" || data === null) {
    throw new Error("Second argument must be a non-null object.");
  }
  if (!(regex instanceof RegExp)) {
    throw new Error("Third argument must be a valid RegExp.");
  }

  return str.replace(regex, (match, key) => {
    if (key in data) {
      const value = data[key];
      return value !== undefined ? String(value) : "";
    } else {
      return match;
    }
  });
};

// 示例 str = '你好（（name））,今天是（（week））,你想吃点什么'
// obj { name:'张三'， week:"星期天" }
// 正则字符（（）） --> 根据自己需求修改正则
